package ai.yunxi.im.route;

import ai.yunxi.im.common.config.BeanConfiguration;
import ai.yunxi.im.common.utils.SpringBeanFactory;
import ai.yunxi.im.common.utils.ZKUtil;
import ai.yunxi.im.route.config.InitConfiguration;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;

import java.util.List;

/**
 * @author 小五-云析学院
 * @date 2020-06-21 14:37
 * @remark
 */
@SpringBootApplication
@Import({SpringBeanFactory.class, ZKUtil.class, BeanConfiguration.class})
public class IMRouteApplication {
    @Autowired
    private InitConfiguration conf;
    @Autowired
    private ZKUtil zkUtil;

    public static void main(String[] args) {
        SpringApplication.run(IMRouteApplication.class, args);
    }

    @Bean
    @Primary
    public ZkClient createZKClient(){
        ZkClient zk = new ZkClient(conf.getAddr());

        //监听/im节点下子节点的变化，实时更新server list
        zk.subscribeChildChanges(conf.getRoot(), new IZkChildListener() {

            @Override
            public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
                zkUtil.setAllNode(currentChilds);
            }
        });
        return zk;
    }
}
